﻿using DevComponents.DotNetBar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLDVu.GUI
{
    public partial class DanhSachDichVu : Office2007Form
    {
        public event EventHandler Button_Clicked;
        public static string NextID(string lastID, string prefixID)
        {
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;
        }
        public DanhSachDichVu()
        {
            InitializeComponent();
        }
        void DichVu()
        {
            var dichvu = from dv in BO.DanhSachDichVuBO.DanhSachDichVu()
                         select new { dv.MaDichVu, dv.TenDichVu, dv.LOAIDICHVU.TenLoaiDichVu, dv.TienCong };
            gvDichVu.DataSource = dichvu;
        }
        private void DanhSachDichVu_Load(object sender, EventArgs e)
        {
            DichVu();

            cbLoaiDichVu.DataSource = BO.DanhSachDichVuBO.DanhSachLoaiDichVu();
            cbLoaiDichVu.DisplayMember = "TenLoaiDichVu";
            cbLoaiDichVu.ValueMember = "MaLoaiDichVu";


            txtTienCong.Text = "0";
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
           
        }

        private void gvDichVu_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int index = e.RowIndex;

                ThemBaoGiaForm.madichvu = gvDichVu.Rows[index].Cells["MaDichVu"].Value.ToString().Trim();
                QuanLyPhieuBaoGia.madichvu = gvDichVu.Rows[index].Cells["MaDichVu"].Value.ToString().Trim();
            }
            catch
            {
                MessageBox.Show("Không có dich vu nào");
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
           
            
        }

        private void txtTienCong_KeyPress(object sender, KeyPressEventArgs e)
        {
           
        }

        private void txtTenDichVuTK_TextChanged(object sender, EventArgs e)
        {
            //if(txtTenDichVu.Text.Trim()!="")
            //{
            //    gvDichVu.DataSource = BO.DanhSachDichVuBO.DanhSachDichVuTheoLoaiDichVu(cbLoaiDichVuTK.SelectedValue.ToString());
            //}
            //else
            //{
            //    gvDichVu.DataSource = BO.DanhSachDichVuBO.DanhSachDichVuTheoTenDichVu(txtTenDichVuTK.Text,cbLoaiDichVuTK.SelectedValue.ToString());
            //}
        }

        private void cbLoaiDichVuTK_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

       

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click_1(object sender, EventArgs e)
        {
            bool kt = false;
            if (txtTenDichVu.Text.Trim() == "")
            {
                MessageBox.Show("Bạn phải nhập tên dịch vụ");
                kt = true;
                txtTenDichVu.Focus();
                return;
            }
            if (txtTienCong.Text.Trim() == "")
            {
                MessageBox.Show("Bạn phải nhập tiền công");
                kt = true;
                txtTienCong.Focus();
                return;
            }
            if (kt == false)
            {
                string madichvu;
                if (BO.DanhSachDichVuBO.DichVuCuoiCung() != null)
                    madichvu = NextID(BO.DanhSachDichVuBO.DichVuCuoiCung().MaDichVu, "DV");
                else
                    madichvu = "DV0001";
                BO.DanhSachDichVuBO.ThemDichVu(madichvu, cbLoaiDichVu.SelectedValue.ToString(), txtTenDichVu.Text, txtTienCong.Text);

                DichVu();
                txtTienCong.Text = "0";
                txtTenDichVu.Text = "";
                txtTenDichVu.Focus();
                MessageBox.Show("Bạn đã thêm thành công");
            }
        }

        private void btnThoat_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChon_Click_1(object sender, EventArgs e)
        {
            if (this.Button_Clicked != null)
            {
                this.Button_Clicked(sender, e);
            }
            this.Close();
        }

        private void txtTienCong_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}
